package Service;

import Dao.*;
import Dao.Impl.AdminDAOImpl;
import Dao.Impl.UserDAOImpl;
import Model.*;
import Util.Utils;
import Util.DbConnector;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

public class simpleBankSystem {
    private Connection connection;

    private UserDAO userDAO;
    private AdminDAO adminDAO;

    private Scanner scanner;
    Utils utils = new Utils();

    public simpleBankSystem() {
        try {
            connection = DbConnector.getConnection();
            userDAO = new UserDAOImpl();
            adminDAO = new AdminDAOImpl();
            scanner = new Scanner(System.in);

        } catch (SQLException e) {
            System.out.println("数据库连接失败: " + e.getMessage());
            System.exit(1);
        }
    }

    public void showMenu() {
        boolean running = true;

        while (running) {
            printLargeTitle();
            System.out.println("=========================");
            System.out.println(">主菜单");
            System.out.println("1. 银行开户");
            System.out.println("2. 用户登录");
            System.out.println("3. 管理员登录");
            System.out.println("0. 退出");
            System.out.println("=========================");
            System.out.print("请选择操作(0-3): ");
            int input = utils.getValidMenuChoice(3);

            switch (input) {
                case 1:
                    addUser();
                    break;
                case 2:
                    userLogin();
                    break;
                case 3:
                    adminLogin();
                    break;
                case 0:
                    running = false;
                    break;
                default:
                    System.out.println("无效的选择，请重新输入。");
                    break;
            }
        }

        scanner.close();
    }

    public void userLogin() {
        int loginAttempts = 0;
        final int MAX_ATTEMPTS = 3;

        while (loginAttempts < MAX_ATTEMPTS) {
            System.out.print("输入用户名: ");
            String username = scanner.nextLine().trim();
            System.out.print("输入密码: ");
            String password = scanner.nextLine().trim();

            try {
                User user = userDAO.getUsrByName(username);
                if (user.getPassword().equals(password)) {
                    System.out.println("登录成功!");
                    new UserSystem(connection).userOperations(user);
                    return;
                } else {
                    System.out.println("用户名或密码错误。");
                    loginAttempts++;
                }
            } catch (SQLException e) {
                System.out.println("登录时出错: " + e.getMessage());
                loginAttempts++;
            } catch (Exception e) {
                System.out.println("发生未知错误: " + e.getMessage());
                loginAttempts++;
            }
        }

        System.out.println("连续错误次数过多，请联系管理员。");
        System.exit(0);
    }


    private void adminLogin() {
        System.out.print("输入管理员用户名: ");
        String username = scanner.nextLine().trim();
        System.out.print("输入管理员密码: ");
        String password = scanner.nextLine().trim();

        Admin admin = adminDAO.getAdmin(username, password);
        if (admin == null) {
            System.out.println("管理员不存在。");
            return;
        }

        if (admin.getPassword().equals(password)) {
            System.out.println("管理员登录成功!");
            new AdminSystem().adminOperations();
        } else {
            System.out.println("管理员用户名或密码错误。");
        }
    }

    public void addUser() {
        try {
            System.out.print("输入用户名: ");

            String name = scanner.nextLine().trim();

            if (name.isEmpty()) {
                System.out.print("用户名不能为空!");
                return;
            }

            while (userDAO.getUsrByName(name) != null) {
                System.out.print("用户已存在，请重新输入用户名: ");
                name = scanner.nextLine().trim();
            }

            System.out.print("输入用户密码: ");
            String pwd = scanner.nextLine().trim();

            while (!utils.isValidPassword(pwd)) {
                System.out.print("密码格式错误，请重新输入: ");
                pwd = scanner.nextLine().trim();
            }

            System.out.print("输入联系方式(邮箱): ");
            String contactInfo = scanner.nextLine().trim();
            while (contactInfo.isEmpty()){
                System.out.print("联系方式不能为空，请重新输入: ");
                contactInfo = scanner.nextLine().trim();
            }

            while (!utils.isValidContactInfo(contactInfo)) {
                System.out.print("联系格式错误，请重新输入: ");
                contactInfo = scanner.nextLine().trim();
            }

            User user = new User(0, name, pwd, contactInfo, 0.00, 0);
            userDAO.addUser(user);
            System.out.println("用户添加成功!");


        } catch (SQLException e) {
            System.out.println("添加客户时出错: " + e.getMessage());
        }
    }

    private static void printLargeTitle() {
        System.out.println("  ____   _                    _        ____                 _    ");
        System.out.println(" / ___| (_) _ __ ___   _ __  | |  ___ | __ )   __ _  _ __  | | __");
        System.out.println(" \\___ \\ | || '_ ` _ \\ | '_ \\ | | / _ \\|  _ \\  / _` || '_ \\ | |/ /");
        System.out.println("  ___) || || | | | | || |_) || ||  __/| |_) || (_| || | | ||   < ");
        System.out.println(" |____/ |_||_| |_| |_|| .__/ |_| \\___||____/  \\__,_||_| |_||_|\\_\\");
        System.out.println("                      |_|                                        ");
    }
}